// pages/geren/geren.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: '',
    user: '',
    userImg: '',
    tipShow: false,
    codeNum: '',
    showCode: false,
    codeImg: ''
  },

  tipConfirm(e) {
    if (this.data.codeNum == '') {
      wx.showToast({
        title: '请输入兑换码',
        icon: 'none'
      })
      return
    }

    console.log(this.data.codeNum);

    var url = app.globalData.url + "doctor/updateUserType",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
        code: that.data.codeNum,
        userName: app.globalData.user.userName,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data

          console.log(datas);
          wx.showToast({
            title: datas.msg,
            icon: 'none'
          })

          if (datas.flag == 'success') {

            app.globalData.userType = datas.userType
            app.globalData.user.userType = datas.userType
            app.globalData.doctor = datas.doctor
            app.globalData.doctorId = datas.doctor.doctorId

            wx.setStorageSync('doctor', JSON.stringify(datas.doctor))
            console.log(app.globalData);
            that.setData({
              userImg: app.globalData.user.userImg,
              user: app.globalData.user,
            })
            console.log(datas.doctor);
            wx.showModal({
              title: '提示',
              content: '您已成功兑换医生身份，请完善您的基本信息',
              showCancel: false,
              complete: (res) => {
                if (res.confirm) {


                }
              }
            })
          }

          that.setData({
            codeNum: ''
          })
        }
      },
      err => {
        console.log(err);
      })
  },


  toPage(e) {
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'record') {
      wx.navigateTo({
        url: '/user/pages/functionPage/functionPage',
      })
    } else if (flag == 'patientList') {
      wx.navigateTo({
        url: '/doctor/pages/patientList/patientList',
      })
    } else if (flag == 'dui') {
      this.setData({
        tipShow: true
      })
    } else if (flag == 'doctorInfo') {
      wx.navigateTo({
        url: '/doctor/pages/updateDoctorInfo/updateDoctorInfo',
      })
    } else if (flag == 'erCode') {
      console.log(this.data.codeImg);
      if (this.data.codeImg != '') {
        this.setData({
          showCode: true
        })
      } else {
        this.getCodeImg()
      }
    } else if (flag == 'yiJian') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    } else if (flag == 'about') {
      wx.showToast({
        title: '功能暂未开启',
        icon: 'none'
      })
    }
  },

  getCodeImg() {
    var url = app.globalData.url + "doctor/listErweima",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        doctorId: app.globalData.doctorId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data

          that.setData({
            showCode: true,
            codeImg: datas
          })
          console.log();

        }
      },
      err => {
        console.log(err);
      })

  },

  onClose() {
    this.setData({
      showCode: false
    })
  },
  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
  },

  errHandle() {
    this.setData({
      userImg: '/img/userImg.jpg'
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({
      userImg: app.globalData.user.userImg,
      user: app.globalData.user,
    })
    console.log(app.globalData.user);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})